#coding=utf-8
'''
Created on 2015��3��14��
@author: wangpeng
'''
import serial
import time
import logging_save
import logging
import sys
import re
#建立serial连接
ser_connection=serial.Serial('com6',57600,bytesize=8)
#可能存在的风险点：检测内存值如果串口在输入其他值，可能会报错，需要继续优化
#检测总的剩余内存值 测试
#ser_connection.write('top -n 1\n')
ser_connection.write('top -n 1\n')
time.sleep(3)
data=''
n=ser_connection.inWaiting()
testfile=open('E:\\huawei\\cpumemory.txt','w+')
if n:
    data += ser_connection.read(n)
    for x in range(len(data)):
        print >>testfile,    data[x],
testfile.close()
def detectmomoerycpu():
    savelog=open('E:\\huawei\\cpumemory.txt','r+')     
    #print savelog.read()
    #读取第二行
    memoryvalue=savelog.readlines()[1]
#得到free 内存的值
#判断内存是否有其他信息
    restring=re.compile(r'f r e e')
    if re.search(restring, memoryvalue):
#读取第二行的第几列数字
        freemomory=memoryvalue[-90:-80].replace(' ','')
        if freemomory>10000:
            logging_save.logging.info('总内存可用检测正常，剩余总内存值为： '+  str(freemomory)+   ' kb')
        else:
            logging_save.logging.warning('FAIL,FAIL,FAIL,up快速调节音量失败'  + str(freemomory) +   ' kb' )  

    else:
        logging.info('系统正在打印其他无用信息，等稍等片刻再测试，谢谢你的理解') 
#关闭打印的串口    
    savelog.close()


#读取CPU的值，抽取idel空余CPU值
    savelog2=open('E:\\huawei\\cpumemory.txt','r+')  
    cpuvalue=savelog2.readlines()[2]
    freecpu=cpuvalue[62:70].replace(' ','')
    savelog2.close()

    restring2=re.compile(r'C P U')
    if re.search(restring2, cpuvalue):
#读取第二行的第几列数字
        freecpu=cpuvalue[62:70].replace(' ','')
        if freecpu>60:
            logging_save.logging.info('剩余CPU检测正常，剩余CPU值为： '+  str(freecpu)+   ' %')
        else:
            logging_save.logging.warning('FAIL,FAIL,FAIL,CPU值为'  + str(freecpu) +   ' %' )  

    else:
        logging_save.logging.info('系统正在打印其他无用信息，等稍等片刻再测试，谢谢你的理解') 

    savelog2.close()
    
ser_connection.close()


if __name__=="__main__":
    detectmomoerycpu()

'''
开机初始值检测
(150319_10:12:47.846)[JMem: 37068K used, 24772K free, 0K shrd, 0K buff, 23916K cached
(150319_10:12:47.847)CPU:   0% usr   9% sys   0% nic  90% idle   0% io   0% irq   0% sirq
(150319_10:12:47.847)Load average: 0.40 0.11 0.03 1/52 321
(150319_10:12:47.847)[7m  PID  PPID USER     STAT   VSZ %VSZ %CPU COMMAND
(150319_10:12:47.847)[0m
(150319_10:12:47.847)  321    28 admin    R     1948   3%   9% top -n 1 
(150319_10:12:47.847)  210   197 admin    S    20816  34%   0% TB-wifiListen 
(150319_10:12:47.847)  215   197 admin    S    20816  34%   0% TB-downBuffer 
(150319_10:12:47.923)  190     1 admin    S    20816  34%   0% main_app 
(150319_10:12:47.924)  235   197 admin    S    20816  34%   0% TB-playThread 
(150319_10:12:47.924)  219   197 admin    S    20816  34%   0% TB-RcCmdS 
(150319_10:12:47.924)  197   190 admin    S    20816  34%   0% main_app 
(150319_10:12:47.924)  236   197 admin    S    20816  34%   0% main_app 
(150319_10:12:47.924)  297   197 admin    S    20816  34%   0% TB-dmrSsdpS 
(150319_10:12:47.924)  296   197 admin    S    20816  34%   0% TB-dmrHttpS 
(150319_10:12:47.924)  320   197 admin    S    20816  34%   0% main_app 
(150319_10:12:48.001)  237   197 admin    S    20816  34%   0% main_app 
(150319_10:12:48.002)  207   197 admin    S    20816  34%   0% TB-keyLoop 
(150319_10:12:48.002)  233   197 admin    S    20816  34%   0% TB-alarmGetTime 
(150319_10:12:48.002)  198   197 admin    S    20816  34%   0% TB-mcServer 
(150319_10:12:48.002)  199   197 admin    S    20816  34%   0% TB-timerLoop 
(150319_10:12:48.002)  300   197 admin    S    20816  34%   0% TB-dmrSsdpR 
(150319_10:12:48.003)  301   197 admin    S    20816  34%   0% TB-dmrCheckIp 
(150319_10:12:48.003)  208   197 admin    S    20816  34%   0% TB-nl 
(150319_10:12:48.079)  209   197 admin    S    20816  34%   0% TB-Linker 

'''